/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示将系统时钟在 HSI 和 HSE 循环切换。
  *          通过时钟输出管脚可观察 PCLK / HSIOSC 的时钟信号
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：断开J21、J22
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    HAL_StatusTypeDef res;

    Bsp_Led_Init();

    HAL_SysCtrl_ClkOut_MCO_PB03(SYSCTRL_MCO_SRC_HSIOSC, SYSCTRL_MCO_DIV2); // PB03输出 48MHz / 2 = 24MHz
    HAL_SysCtrl_ClkOut_PCLK_PA02();                                   // 通过PA02观察PCLK频率

    while (1)
    {
        //--------------------------------------------------------
        // 以下从HSI4M切换到HSE
        res = HAL_SysCtrl_HSE_Enable_Crystal(8000000, SYSCTRL_HSE_DRIVER_3); // 使能HSE时钟，需要根据晶体型号选择合适的驱动
        while (res == HAL_ERROR)
        {
            BSP_LED_TOG();                                            // 起振失败
        }

        HAL_SysCtrl_ConfigWaitCycle(8000000);                         // 设置WaitCycle
        if (HAL_SysCtrl_SysClk_Switch(SYSCTRL_SYSCLKSRC_HSE) == SYSCTRL_SYSCLKSRC_HSE) // 切换系统时钟到外部高速时钟成功
        {
            BSP_LED_SETHIGH();
            HAL_SysCtrl_HSI_Disable();                                // 关闭HSI
            FirmwareDelay(800 * 500);                                 // Delay 500ms
        }
        else
        {
            while (1)
            {
                BSP_LED_TOG();                                        // 切换失败
            }
        }

        //--------------------------------------------------------
        // 以下从HSE切换到HSI4M
        HAL_SysCtrl_HSI_Enable(HSIOSC_TO_HSI4MHZ);
        if (HAL_SysCtrl_SysClk_Switch(SYSCTRL_SYSCLKSRC_HSI) == SYSCTRL_SYSCLKSRC_HSI) // 切换系统时钟到内部高速时钟成功
        {
            BSP_LED_SETLOW();
            HAL_SysCtrl_HSE_Disable();                                // 关闭HSE
            HAL_SysCtrl_ConfigWaitCycle(4000000);                     // 修改FlashWait
            FirmwareDelay(400 * 500);                                 // Delay 500mS
        }
        else
        {
            while (1)
            {
                BSP_LED_TOG();
            }
        }
    }
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
